// pages/addaddress/addaddress.js

const {
  Address
} = require("../../utils/api")
const {
  globalData
} = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addAddr: [],
    provinceArr: [], //省份数据
    cityArr: [], //市数据
    areaArr: [], //区数据
    provinceName: "", //省份名称
    cityName: "", //市名称
    areaName: "", //区名称
    privId: "", //省份id
    cityId: "", //市id
    areaId: "", //区id
    value: [],
    flag: false,
    username: "", //姓名
    tel: "", //电话号
    addList: [], //地址列表
    setAdd:[]    //需要设置的地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //所有地址列表
    this.getAllAddList()
    console.log(globalData.addIndex)
    //获取点击的index
    let index=globalData.addIndex
    console.log(index)
    console.log(globalData.addList[index])
    console.log(globalData.addList[index].username)
    this.setData({
      username:globalData.addList[index].receiver,
      tel:globalData.addList[index].mobile,
      provinceName: globalData.addList[index].province, //省份名称
      cityName: globalData.addList[index].city, //市名称
      areaName: globalData.addList[index].area, //区名称
    })
    this.initAddress()
    
  },
  //底部弹出
  onOpen() {
    console.log("11")
    this.setData({
      flag: true
    })
  },

  //表单
  changeLocation(e) {

    let value = e.detail.value
    this.setData({
      value: value
    })
    console.log(this.data.value[0])
    console.log(this.data.value)
    this.initAddress()
    this.getCityData()
    console.log(this.data.provinceArr, this.data.cityArr, this.data.areaArr)
  },
  //获取省数据
  initAddress(privId, cityId, areaId) {
    const data = {
      pid: 0
    }
    Address.getAddressList(data, (res) => {
      console.log(res.data[2].areaName)
      let index = this.data.value[0]
      console.log(index)
      this.setData({
        provinceArr: res.data,
      })
      this.getCityData(privId ? privId : res.data[index ? index : 0].areaId, cityId, areaId)
    })
    console.log(this.data.provinceName)
  },

  //获取城市数据
  getCityData(privId, cityId, areaId) {
    const data = {
      pid: privId
    }
    Address.getAddressList(data, (res) => {
      // console.log(res.data)
      this.setData({
        cityArr: res.data
      })
      let index = this.data.value[1]
      this.getAreaData(cityId ? cityId : res.data[index ? index : 0].areaId, areaId)
    })
  },

  //获取区
  getAreaData(cityId, areaId) {
    const data = {
      pid: cityId
    }
    Address.getAddressList(data, (res) => {
      // console.log(res.data)
      this.setData({
        areaArr: res.data
      })
      console.log(this.data.areaArr)
    })

  },
  //点击确定
  determine() {
    this.setData({
      flag: false
    })
    console.log(this.data.value)
    let index0 = this.data.value[0]
    let index1 = this.data.value[1]
    let index2 = this.data.value[2]
    console.log(111)
    console.log(this.data.provinceArr[index0].areaId)
    console.log(this.data.cityArr[index1].areaId)
    console.log(this.data.areaArr[index2].areaId)
    this.setData({
      provinceName: this.data.provinceArr[index0].areaName,
      cityName: this.data.cityArr[index1].areaName,
      areaName: this.data.areaArr[index2].areaName,
      privId: this.data.provinceArr[index0].areaId, //省份id
      cityId: this.data.provinceArr[index1].areaId, //市id
      areaId: this.data.provinceArr[index2].areaId, //区id
    })

  },
  //姓名
  oninpaddr(event) {
    this.setData({
      username: event.detail.value
    })
  },
  //电话号
  oninptel(event) {

    this.setData({
      tel: event.detail.value
    })
    console.log(this.data.tel)
  },
  //保存收货地址
  baoAdd(e) {
    let index=globalData.addIndex
    console.log(globalData.addList[index].addrId,"========================")
    let addrId=globalData.addList[index].addrId
    console.log(e)
    console.log( this.data.username,this.data.tel)
    let addrParam = {
      addr: this.data.provinceName,
      addrId: addrId,
      area: this.data.areaName,
      areaId: this.data.areaId,
      city: this.data.cityName,
      cityId: this.data.cityId,
      mobile: this.data.tel,
      province: this.data.provinceName,
      provinceId: this.data.privId,
      receiver: this.data.username
    }
    Address.updateAdd(addrParam,(res)=>{
      console.log(res)
      // if(res.statusCode == 200){
      //   wx.showToast({
      //     title: '添加地址成功',
      //   })
      //   wx.redirectTo({
      //     url: '/pages/address/address',
      //   })
      // }

    })
  },

  //删除收货地址
  delAdd() {
    console.log("删除")
    let index=globalData.addIndex
    console.log(globalData.addList[index].addrId,"========================")
    let addrId=globalData.addList[index].addrId
    Address.delAddress(addrId,(res)=>{
      console.log(res)
      if(res.statusCode==200){
        wx.showToast({
          title: '删除成功',
        })
        wx.navigateTo({
          url: '/pages/address/address',
        })
        // wx.redirectTo({
        //   url: '/pages/address/address',
        // })
      }
    })
  },
  //获取所有地址列表
  getAllAddList() {
    Address.getAddList((res) => {
      console.log(res)
      globalData.addList=res.data
      this.setData({
        addList: res.data
      })
    })
    console.log(this.data.addList)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})